package com.cxl.parser;

import java.util.ArrayList;
import java.util.List;

import org.htmlparser.Node;
import org.htmlparser.NodeFilter;
import org.htmlparser.Tag;
import org.htmlparser.filters.AndFilter;
import org.htmlparser.filters.HasAttributeFilter;
import org.htmlparser.filters.HasParentFilter;
import org.htmlparser.filters.TagNameFilter;
import org.htmlparser.util.NodeList;


public class GrabDangDang extends Grab implements InfoGrab<String>{

	//String encode = "UTF-8";
	
	

	public NodeFilter getFilter() {
		return new AndFilter(new TagNameFilter("span"),new HasAttributeFilter("class", "num"));
	}

	public NodeFilter getPictureFilter() {
		
		//img src="http://img34.ddimg.cn/92/24/60038444-1_h.jpg" alt="" id="largePic"/>
		return new AndFilter(new TagNameFilter("img"),
				new HasAttributeFilter("id", "largePic"));
	}

	public List<String> searchGoodsUrl(String url) {
		List<String> list = new ArrayList<String>();
	
			NodeFilter filter = getSearchUrlFiter();
			
			NodeList listnode = getNodesByFilter(url, filter, -1);
			//第一次搜索查询不到的话
			
			
			if(listnode.size()==0)
			{
				//当当搜索不到 是进行了 重定向  
				String html=getHtmlCode(url);
				//提取里面的重定向的链接 <script>window.location.href="http://search.dangdang.com/search.php?key=%C3%C0%B5%C4%20PCJ405&kl=%B0%D9%BB%F5&kl_id=&stop=1"</script>
				url=html.split("\\\"")[1];
				filter=getSearchUrlFiter1();
				listnode = getNodesByFilter(url, filter, -1);
			}

			if (listnode.size() > 0) {
				for (int i = 0; i < (listnode.size()); i++) {
					Node textnode = listnode.elementAt(i);
					Tag a = (Tag) textnode;
					//System.out.println(a.getAttribute("href"));
					list.add(a.getAttribute("href"));
				}
			}
			
		return list;
	}

	
	
	public NodeFilter getSearchUrlFiter() {
		NodeFilter filter=new  AndFilter(new TagNameFilter("div"),
					new HasAttributeFilter("class", "name"));
		NodeFilter filter1=new  AndFilter(new TagNameFilter("a"),
				new HasParentFilter(filter));
		return filter1;
	}
	
	
	
	/**
	 * 第2种搜索方式 查询不到的情况
	 * @return
	 */
	public NodeFilter getSearchUrlFiter1() {
		NodeFilter filter=new  AndFilter(new TagNameFilter("li"),
					new HasAttributeFilter("class", "maintitle"));
		NodeFilter filter1=new  AndFilter(new TagNameFilter("a"),
				new HasParentFilter(filter));
		return filter1;
	}

	public NodeFilter getNextFilter() {
		// TODO Auto-generated method stub
		return null;
	}

	public NodeFilter getContentFilter() {
		// TODO Auto-generated method stub
		return null;
	}

	public NodeFilter getContentFilter1() {
		// TODO Auto-generated method stub
		return null;
	}

	public NodeFilter getContentFilter2() {
		// TODO Auto-generated method stub
		return null;
	}


	public NodeFilter getTitleFilter() {
		return new TagNameFilter("h1");
	}

	public NodeFilter getNotExistsFilter() {
		// TODO Auto-generated method stub
		return null;
	}

	public NodeFilter getBrandFilter() {
		// TODO Auto-generated method stub
		return null;
	}




	



}
